<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main_building extends CI_Controller {

	 function __construct()
    {
        parent::__construct();
    }
	public function index($sequence="")
	{
		$html=$this->init("基礎資料-主要建材","jquery,pagination,base_data","table");
		$this->load->model('main_building_model');
		$keyword="";
		$config['per_page'] = 10;
		$config['total_rows'] = $this->main_building_model->get_all_count();
		$config['url'] = '/main_building/page_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit =$config['per_page'].",0";
		$html['page'] =$config;
		$html['sequence']=$sequence;
		$html["main_building"]=$this->main_building_model->query_keyword($keyword,$limit);
		$this->load->view('main_building',$html);
	}

	public function page_ajax()
	{
		$this->load->model('main_building_model');
		$page=$this->input->post('page');		
		$sequence=$this->input->post('sequence');
		$keyword="";
		$html["edit_mode"]="";
		$config['per_page'] = 10;
		$p=$config['per_page']*($page-1);
		$limit =$config['per_page'].",".$p;
		$html["result"]=$this->main_building_model->query_keyword($keyword,$limit);
		$this->load->view('main_building_ajax',$html);
	}

	public function add()
	{
		$this->load->model('main_building_model');
		$data['value']=$this->input->post('value');
		$data['explanation']=$this->input->post('explanation');
		$data['sequence']=$this->input->post('sequence');
		$return_result=$this->main_building_model->add($data);
		if($return_result>0){
			redirect("/main_building");
		}else{
			echo "插入失敗";
		}
	}
	public function edit_ajax() {
		$this->load->model('main_building_model');
		$id=$this->input->post('main_building_id');
		$html["edit_mode"]="edit";
		$html["info"]=$this->main_building_model->get_info($id);
		$this->load->view('main_building_ajax',$html);
	}
	public function edit_submit_ajax() {
		$this->load->model('main_building_model');
		$data['id']=$this->input->post('main_building_id');
		$data['value']=$this->input->post('value');
		$data['explanation']=$this->input->post('explanation');
		$data['sequence']=$this->input->post('sequence');
		$data['mtime']=time();
		$return_result=$this->main_building_model->update_data($data);
		if($return_result){
			$html["edit_mode"]="edit_return";
			$html["info"]=$this->main_building_model->get_info($data['id']);
			$this->load->view('main_building_ajax',$html);
		}else{
			echo "0";
		}
	}
	
	public function cancel_edit_ajax() {
		$this->load->model('main_building_model');
		$id=$this->input->post('main_building_id');
		$html["edit_mode"]="edit_return";
		$html["info"]=$this->main_building_model->get_info($id);
		$this->load->view('main_building_ajax',$html);
	}
	public function data_delete($id=null){
		$this->load->model('main_building_model');
		if($id!=null)
		{
			$data["id"]=$id;
			$return_result=$this->main_building_model->del($data);
			if($return_result){
				redirect("/main_building");
			}else{
				echo "刪除失敗";
			}
		}else{
			echo "刪除失敗";
		}	
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */